package com.feisuanyz.lyweb01.service.impl;

import com.feisuanyz.lyweb01.dao.CooperativePurchaseMapper;
import com.feisuanyz.lyweb01.pojo.domain.CooperativePurchaseDO;
import com.feisuanyz.lyweb01.pojo.dto.CooperativePurchaseDTO;
import com.feisuanyz.lyweb01.pojo.query.CooperativePurchaseQuery;
import com.feisuanyz.lyweb01.pojo.vo.PageResult;
import com.feisuanyz.lyweb01.service.CooperativePurchaseService;
import java.lang.Boolean;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 合作购买的实现
 *
 * @author deng
 * @date 2025-02-26 11:57:37
 */
@Service
public class CooperativePurchaseServiceImpl implements CooperativePurchaseService {

    @Autowired
    private CooperativePurchaseMapper cooperativePurchaseMapper;

    @Override
    public PageResult<CooperativePurchaseDO> getCooperativePurchaseList(CooperativePurchaseQuery query) {
        IPage<CooperativePurchaseDO> page = new Page<>(query.getPageIndex(), query.getPageSize());
        IPage<CooperativePurchaseDO> purchasePage = cooperativePurchaseMapper.selectPage(page, Wrappers.emptyWrapper());
        PageResult<CooperativePurchaseDO> pageResult = new PageResult<>(purchasePage);
        return pageResult;
    }

    @Override
    public CooperativePurchaseDO getCooperativePurchaseInfo(CooperativePurchaseQuery query) {
        CooperativePurchaseDO purchaseDO = cooperativePurchaseMapper.selectById(query.getPurchaseId());
        return purchaseDO;
    }

    @Override
    public PageResult<CooperativePurchaseDO> searchCooperativePurchase(CooperativePurchaseQuery query) {
        QueryWrapper<CooperativePurchaseDO> wrapper = Wrappers.<CooperativePurchaseDO>query().like(!query.getKeyword().isEmpty(), "purchase_title", query.getKeyword()).or().like(!query.getKeyword().isEmpty(), "purchase_description", query.getKeyword());
        IPage<CooperativePurchaseDO> page = new Page<>(query.getPageIndex(), query.getPageSize());
        IPage<CooperativePurchaseDO> purchasePage = cooperativePurchaseMapper.selectPage(page, wrapper);
        PageResult<CooperativePurchaseDO> pageResult = new PageResult<>(purchasePage);
        return pageResult;
    }

    @Override
    public Boolean addCooperativePurchase(CooperativePurchaseDTO dto) {
        CooperativePurchaseDO purchaseDO = new CooperativePurchaseDO();
        purchaseDO.setPurchaseTitle(dto.getPurchaseTitle());
        purchaseDO.setPurchaseDescription(dto.getPurchaseDescription());
        purchaseDO.setCreateBy(dto.getCreateBy());
        int result = cooperativePurchaseMapper.insert(purchaseDO);
        return result > 0;
    }

    @Override
    public Boolean updateCooperativePurchase(CooperativePurchaseDTO dto) {
        CooperativePurchaseDO purchaseDO = new CooperativePurchaseDO();
        purchaseDO.setPurchaseId(dto.getPurchaseId());
        purchaseDO.setPurchaseTitle(dto.getPurchaseTitle());
        purchaseDO.setPurchaseDescription(dto.getPurchaseDescription());
        purchaseDO.setUpdateBy(dto.getUpdateBy());
        int result = cooperativePurchaseMapper.updateById(purchaseDO);
        return result > 0;
    }

    @Override
    public Boolean deleteCooperativePurchase(CooperativePurchaseDTO dto) {
        int result = cooperativePurchaseMapper.deleteById(dto.getPurchaseId());
        return result > 0;
    }
}
